import { addBizExchangeRateApi, updateBizExchangeRateApi } from "@/api/configCenter/exchangeRate/index";
import { message } from "ant-design-vue";

export default function OperationHook(state: any) {
  const { passData, type, formState, loading, formRef, emit, dataDict } = state;

  const onSubmit = () => {
    formRef.value.validate().then(() => {
      const { name } = dataDict.value["CURRENCY"].find((t: any) => t.code === formState.currencyCode) || {};
      formState.currencyName = name;
      loading.value = true;
      let requestFn;
      const params = {
        ...formState
      };

      if (type.value === 1) {
        requestFn = addBizExchangeRateApi;
      } else {
        params.id = passData.value.id;
        requestFn = updateBizExchangeRateApi;
      }

      requestFn(params)
        .then((res: any) => {
          const { code } = res;
          if (code === 200) {
            message.success("操作成功!");
            emit("submit");
          }
        })
        .finally(() => {
          loading.value = false;
        });
    });
  };
  const resetForm = () => {
    formRef.value.resetFields();
    emit("submit");
  };
  return {
    resetForm,
    onSubmit
  };
}
